package com.fh.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.fh.model.Images;
import com.fh.service.ImagesService;
import com.fh.util.Upload;
@Controller
@RequestMapping(value = "/ImagesController")
public class ImagesController {
	@Autowired
	private ImagesService imagesService;

	@RequestMapping("/index")
	public String index(ModelMap modelMap) {
		List<Images> list=imagesService.selectByAll();
		modelMap.put("list", list);
		return "results/index";
	}
//	@RequestMapping("/add")
//	public String add(@RequestParam(value = "fileImg", required = false) MultipartFile fileImg,String title, HttpServletRequest request, ModelMap model) {
//		if(fileImg!=null){
//			Upload upload=new Upload(request);
//			String imgName=upload.upload(fileImg);
//			if(imgName!=null){
//				Images entity=new Images(null, title, imgName, 1);
//				imagesService.insertSelective(entity);
//				model.put("msg", "保存成功");
//			}
//		}
//		return "results/add";
//	}
	
	@RequestMapping("/add")
	public String add(@RequestParam(value = "fileImg", required = false) MultipartFile[] fileImg,String title, HttpServletRequest request, ModelMap model) {
		if(fileImg!=null){
			Upload upload=new Upload(request);
			List<String> imgName=upload.uploads(fileImg);
			if(imgName!=null&&imgName.size()>0){
				for (String string : imgName) {
					Images entity=new Images(null, title, string, 1);
					imagesService.insertSelective(entity);
				}
				model.put("msg", "保存成功");
			}
		}
		return "results/add";
	}
	
	
	@RequestMapping("/delete")
	public String delete(ModelMap model,Long id){
		int i=imagesService.deleteByPrimaryKey(id);
		if(i>0){
			model.put("msg", "删除成功");
		}else{
			model.put("msg", "删除失败");
		}
		//调用集合
		return index(model);
	}
}
